package com.clkj.sunhelp.http;

import com.clkj.sunhelp.common.Config;
import com.clkj.sunhelp.util.CommonUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * Created by Administrator on 2016/3/15.
 */
public class HttpRequest {

    /*public static final String BASE_URL_TEST = "http://192.168.2.142/";
    public static final String BASE_URL_REAL = "http://222.184.118.58:10067/";
    public static final String BASE_URL_REAL_2 = "http://211.149.250.152:6002/";
    public static final String BASE_URL_REAL_3 = "http://admin.hdtcom.com/";
    public static final String BASE_URL = " http://115.29.76.139:8888/";

    public static final String BASE_URL_IN_USE  = BASE_URL_REAL_3;

    public static final String BASE_URL_IN_USE_DOWN_FILE  = "http://admin.hdtcom.com";*/

    //上传处理图片
    public static void uploadProcessPics(String processName, String processId, String userId,List<File> fileList, AsyncHttpResponseHandler responseHandler)
    {

     /*   string content, string userid*/
        RequestParams params = new RequestParams();
        params.put("proc",processName);
        params.put("processid",processId);
        params.put("uId",userId);
        for (int i=0;i<fileList.size();i++)
        {
            try {
                File file =  fileList.get(i);
                params.put("Filedata"+(i+1),file, CommonUtil.guessMimeType(file.getName()),file.getName());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        TwitterRestClient.postByAbsoluteUrl(Config.URL_BASE_IN_USE+"/UploadifyFile", params, responseHandler);
    }



    //上传处理图片
    public static void uploadFengXianControlInfo(String processName, String processId,String projId, String userId,List<File> fileList, AsyncHttpResponseHandler responseHandler)
    {

     /*   string content, string userid*/
        RequestParams params = new RequestParams();
        params.put("proc",processName);
        params.put("procid",processId);
        params.put("processid",projId);
        params.put("uId",userId);
        for (int i=0;i<fileList.size();i++)
        {
            try {
                File file =  fileList.get(i);
                params.put("Filedata"+(i+1),file, CommonUtil.guessMimeType(file.getName()),file.getName());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        TwitterRestClient.postByAbsoluteUrl(Config.URL_BASE_IN_USE+"/tbRiskUploadPhoto", params, responseHandler);
    }

    /*//评论商户
    public static void commentSaler(String orderid, String CommentContent, String CommentLevel, String UserID,boolean isNiMing,List<File> fileList, AsyncHttpResponseHandler responseHandler)
    {

        RequestParams params = new RequestParams();
        params.put("orderid",orderid);
        params.put("CommentContent",CommentContent);
        params.put("CommentLevel","5");
        params.put("UserID",UserID);
        params.put("IsAnonymous",isNiMing);
        for (int i=0;i<fileList.size();i++)
        {
            try {
                File file =  fileList.get(i);
                params.put("Filedata"+(i+1),file, CommonUtil.guessMimeType(file.getName()),file.getName());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        TwitterRestClient.postByAbsoluteUrl(BASE_URL_IN_USE +"ClientApi/Api_SubmitComment", params, responseHandler);
    }

    *//**
     * 上传头像
     * @param UserId
     * @param headPicFile
     * @param responseHandler
     *//*
    public static void uploadHeadPic(String UserId,File headPicFile, AsyncHttpResponseHandler responseHandler)
    {

        RequestParams params = new RequestParams();
        params.put("UserId",UserId);
        try {
            params.put("HeadPicFile",headPicFile, CommonUtil.guessMimeType(headPicFile.getName()),headPicFile.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        TwitterRestClient.postByAbsoluteUrl(BASE_URL_IN_USE +"ClientApi/UploadHeadPic", params, responseHandler);
    }



    //下载广告图片
    public static void downAdsPic(Context context, final List<AppAdsInfo.PiclistEntity> picList, final int adsPicVersion, DownMultiFileCallBack downMultiFileCallBack)
    {
        final AdsPicDao adPicDao = new AdsPicDao(context);

        //已经获取到的图片的数量
        final int[] hasGetFileNum = {0};

        final File[] picFile = new File[picList.size()];
        for (int i = 0 ;i< picList.size();i++)
        {
            final String fileName = picList.get(i).getImgpath().substring(picList.get(i).getImgpath().lastIndexOf("/") + 1);
            final int nowPos = i;

            if (!FileUtils.checkFileExists(Config.APP_BASE_FOLDER)) {
                FileUtils.createDirectory(Config.APP_BASE_FOLDER);
            }

            if (!FileUtils.checkFileExists(Config.APP_BASE_FOLDER + Config.IMAGE_FOLDER)) {
                FileUtils.createDirectory(Config.APP_BASE_FOLDER + Config.IMAGE_FOLDER);
            }

            //如果没有这个图片，则重新下载
            if (!FileUtils.checkFileExists(Config.APP_BASE_FOLDER + Config.IMAGE_FOLDER + File.separator +fileName )) {
                LogUtil.e("sync get file i=:"+i);
                TwitterRestClient.getFileByAbsoluteUrl(BASE_URL_IN_USE + picList.get(i).getImgpath(), new BinaryHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] binaryData) {

                        //如果压缩的包里面不存在被压缩的图片，则创建压缩图片，如果存在，则直接取
                        if (!FileUtils.checkFileExists(Config.APP_BASE_FOLDER + Config.IMAGE_FOLDER + File.separator +fileName )) {
                            picFile[nowPos] = FileUtils.createFile(Environment.getExternalStorageDirectory() + Config.APP_BASE_FOLDER + Config.IMAGE_FOLDER, fileName);
                            LogUtil.e("binaryData:", "共下载了：" + binaryData.length);
                            Bitmap bmp = BitmapFactory.decodeByteArray(binaryData, 0,
                                    binaryData.length);
                            // 压缩格式
                            Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
                            // 压缩比例
                            int quality = 70;
                            try {
                                OutputStream stream = new FileOutputStream(picFile[nowPos]);
                                // 压缩输出
                                bmp.compress(format, quality, stream);
                                // 关闭
                                stream.close();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }

                        //下载完了保存广告图片信息到本地数据库
                        AdsPicItem adsPicItem = new AdsPicItem();
                        adsPicItem.setFilePath(picFile[nowPos].getAbsolutePath());
                        adsPicItem.setAdsVersion(adsPicVersion);
                        adPicDao.add(adsPicItem);
                        LogUtil.e("sync add to db fileinfo=:"+adsPicItem.toString());
                        hasGetFileNum[0]++;
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) {

                    }

                    @Override
                    public void onProgress(long bytesWritten, long totalSize) {
                        super.onProgress(bytesWritten, totalSize);

                    }

                });

            }else {
                //下载完了保存广告图片信息到本地数据库
                AdsPicItem adsPicItem = new AdsPicItem();
                adsPicItem.setFilePath(picFile[nowPos].getAbsolutePath());
                adsPicItem.setAdsVersion(adsPicVersion);
                adPicDao.add(adsPicItem);
                LogUtil.e("add to db adspic:"+adsPicItem.toString());
                hasGetFileNum[0]++;
            }
        }


        if (hasGetFileNum[0] == picList.size())
        {
            downMultiFileCallBack.onSuccess();
        }else {
            downMultiFileCallBack.onError(Config.TIP_DOWN_FILE_ERROR);
        }

    }*/
}
